package org.example.service;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JButton btnLogin;
    private JLabel lblStatus;

    public LoginFrame() {
        setTitle("登录");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        txtUsername = new JTextField(20);
        txtPassword = new JPasswordField(20);
        btnLogin = new JButton("登录");
        lblStatus = new JLabel();

        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });

        add(new JLabel("用户名:"));
        add(txtUsername);
        add(new JLabel("密码:"));
        add(txtPassword);
        add(btnLogin);
        add(lblStatus);
    }

    private void login() {
        String username = txtUsername.getText();
        String password = new String(txtPassword.getPassword());

        // 这里应该添加验证用户名和密码的逻辑
        // 假设用户名为"admin"，密码为"123456"
        if ("admin".equals(username) && "123456".equals(password)) {
            lblStatus.setText("登录成功");
            // 登录成功后，隐藏登录界面并显示FileMergerApp界面
            hide();
            FileMergerApp fileMergerApp = new FileMergerApp();
            fileMergerApp.setVisible(true);
        } else {
            lblStatus.setText("用户名或密码错误");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginFrame().setVisible(true);
        });
    }
}